package com.easy.service.impl;

import com.easy.bean.Admin;
import com.easy.bean.User;
import com.easy.mapper.AdminDao;
import com.easy.mapper.UserDao;
import com.easy.service.LoginService;
import com.easy.util.PasswordUtil;
import com.easy.util.SYS;
import com.easy.util.TokenUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
@Transactional(rollbackFor = Exception.class)
public class LoginServiceImpl implements LoginService {

    private static final String CACHE_NAME = "user";

    @Autowired
    AdminDao adminDao;
    @Autowired
    UserDao userDao;


    public Admin logininfo(String token) {
        //1.token中存的管理员 id
        Object id=TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
        if(id==null){
          throw new RuntimeException("登录异常");
        }
        Admin admin=adminDao.one(Integer.parseInt(id.toString()));
        return admin;
     }


     public User userinfo(String token){
        //1.token中存得用户 id
        Object id=TokenUtil.getOption(token, SYS.TOKEN_KEY_ID);
        if(id==null){
          throw new RuntimeException("登录异常");
        }
        User user=userDao.one(Integer.parseInt(id.toString()));
        return user;
     }
    @Override

    public String login(Admin admin) {
        // 1.通过管理员账号查询数据库
        Admin result = adminDao.getByName(admin.getAdminname());
        if (result == null) {
            throw new RuntimeException("登录异常：管理员不存在");
        }

        // 2.验证密码
        String password = admin.getPassword();
        String encryptionPassword = result.getPassword();
        if (!PasswordUtil.checkPassword(password, encryptionPassword)) {
            throw new RuntimeException("登录异常：密码错误");
        }

        // 3.返回结果（可扩展成token或其他信息）
        String token= TokenUtil.getToken(result.getId(),"admin");
        return token;
    }
    @Override

    public String loginUser(User user) {
        // 1.通过用户账号查询数据库
        User result = userDao.getByName(user.getUsername());
        if (result == null) {
            throw new RuntimeException("登录异常：用户不存在");
        }

        // 2.验证密码
        String password = user.getPassword();
        String encryptionPassword = result.getPassword();
        if (!PasswordUtil.checkPassword(password, encryptionPassword)) {
            throw new RuntimeException("登录异常：密码错误");
        }

        // 3.返回结果（可扩展成token或其他信息）
        String token= TokenUtil.getToken(result.getId(),"user");
        return token;
    }
    @Override
    public Admin getAdminByEmail(String email) {
        return adminDao.getByEmail(email);
    }
    @Override

    public User getUserByEmail(String email) {
        return userDao.getByEmail(email);
    }
    @Override

    public String loginByEmailCode(Admin admin) {
        String token= TokenUtil.getToken(admin.getId(),"admin");
        return token;
    }
    @Override

    public String loginUserByEmailCode(User user) {
        String token= TokenUtil.getToken(user.getId(),"user");
        return token;
    }
}
